package com.espirit.eap.manager;

import java.util.Map;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.espirit.eap.ValidationUtil;
import com.espirit.eap.manager.workflow.WFStatus;
import com.googlecode.cswish.struts.spring.CacheService;
import com.googlecode.cswish.struts.spring.GenericService;

@Service
public class WFStatusManager {
	@Resource
	GenericService genericService;
	
	@Resource
	CacheService cacheService;
	
	@Resource
	ValidationUtil genericServiceUtil;
	
	private Map<String,WFStatus> statuses ;
	
	@PostConstruct
	private void init() {
		statuses = cacheService.newCache(WFStatus.class);
	}
	
	public WFStatus getWFStatus(String wfStatusCode){
		WFStatus wfStatus = new WFStatus();
		wfStatus.setCode(wfStatusCode);
		return genericService.searchAll(wfStatus).getData(0);
	}
	
	public WFStatus getWFStatus(WFStatus wfStatus){
		return genericService.load(wfStatus);
	}
	
	
	public WFStatus getAccept(){
		return getStatus(WFStatus.ACCEPT);
	}
	
	public WFStatus getReject(){
		return getStatus(WFStatus.REJECT);
	}
	
	private WFStatus getStatus(String statusCode){
		if (statuses.get(statusCode) == null){
			WFStatus model = new WFStatus();
			model.setCode(statusCode);
			model = genericService.searchAll(model).getData(0);
			statuses.put(statusCode, model);
		}
		return statuses.get(statusCode);
	}
	
	public void initData(){
		addWFStatus(new WFStatus(1,WFStatus.ACCEPT,"Accept","批准"));
		addWFStatus(new WFStatus(2,WFStatus.REJECT,"Reject","拒绝"));
	}
	private void addWFStatus(WFStatus model){
		genericServiceUtil.checkAndAdd(model);
	}
}
